package com.jun.rabbitmq.producer.config;

import com.jun.common.rabbitmq.constant.ExchangeConstant;
import com.jun.common.rabbitmq.constant.QueueConstant;
import com.jun.common.rabbitmq.constant.RouteConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yuejun.li
 * @date 2024/4/18 13:53:57
 */
@Configuration
public class TopicRabbitConfig {

    @Bean
    public Queue firstQueue(){
        return new Queue(QueueConstant.MAN);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(QueueConstant.WOMAN);
    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(ExchangeConstant.TOPIC_EXCHANGE);
    }


    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    public Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(RouteConstant.TOPIC_MAN);
    }

    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    public Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with(RouteConstant.TOPIC_ALL);
    }
}
